/**
 * 
 */
package com.lfd.controller;

import java.util.ArrayList;



import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lfd.entity.CarProduct;
import com.lfd.service.ICarProductService;
import com.lfd.util.Result;
import com.lfd.util.UserUtil;
import com.lfd.vo.CarProductVo;

/**
 * @author 2322
 *
 */
@Controller
@RequestMapping("/carproduct")
public class CarProductController {

	private final Logger logger=Logger.getLogger(CarProductController.class);
	@Autowired
	private ICarProductService icarProductService;
	
	@ResponseBody
	@RequestMapping("/selectall")
	public Result selectAll(CarProduct carProduct, @RequestParam(value = "page",defaultValue = "1")Integer page,@RequestParam(value = "limit",defaultValue = "10")Integer limit) {
		List<CarProductVo> list = new ArrayList();
		int count=icarProductService.queryCount();
		try {
			
			list = icarProductService.selectCarProductVos(carProduct,page, limit);
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("失败", e);
			return new Result("1", "失败");
		}
		return new Result("0", "成功", count, list);
	}
	
	@ResponseBody
	@RequestMapping("/insertProduct")
	public Result insertProduct(CarProduct carProduct) {
		carProduct.setUserId(UserUtil.getId());
		try {
			icarProductService.insertProduct(carProduct);
			
		} catch (Exception e) {
			logger.error("加入购物车失败", e);
			return new Result("0", "加入购物车失败");
		}
		return new Result("0", "加入购物车成功");
	}
	
	
	@ResponseBody
	@RequestMapping("/upd")
	public Result update(CarProduct carProduct) {
		try {
			icarProductService.updateProduct(carProduct);
		} catch (Exception e) {
			logger.error("失败", e);
			return new Result("1","失败",1,null);
		}
		return new Result("0","成功",1,null);
	}
	
	
	
	@ResponseBody
	@RequestMapping("/hjk")
	public Result deleteById(Long carId) {
		try {
		icarProductService.deleteById(carId);
		} catch (Exception e) {
		logger.error("删除失败",e);
		return new Result("1","失败");
		}
		
		return new Result("0","成功");
	}
	@ResponseBody
	@RequestMapping("/qingkong")
	public Result qingkong() {
		CarProduct carProduct = new CarProduct();
		carProduct.setUserId(UserUtil.getId());
		try {
			icarProductService.qingkong(carProduct);
			return new Result("0", "清空购物车成功");
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("查询失败", e);
			return new Result("1", "清空购物车失败");
		}
	
}
	}